﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Jy123.Web.Manage.Album
{
    public partial class Photo : MgConfig
    {
        protected int pagesize = 10;
        protected int cpage =1;//当前页
        protected List<int?> count;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("AlbumList");
            if (Request.QueryString["page"] != null)
            {
                cpage = Convert.ToInt32(Request.QueryString["page"]);
            }
            count = new List<int?>();
            RptData.DataSource = new BLL.User.Album().ManagePhotoList(cpage, pagesize, "",out count);
            RptData.DataBind();
        }
        /// <summary>
        /// 是否
        /// </summary>
        /// <returns></returns>
        protected string isno(object isbool)
        {
            bool ISbool = (bool)isbool;
            if (ISbool)
                return "<a class=\"cred\">是</a>";
            else
                return "否";
        }
        /// <summary>
        /// 显示文件大小
        /// </summary>
        /// <param name="kb"></param>
        /// <returns></returns>
        protected string viewFileSize(object b)
        {
            int bt = Convert.ToInt32(b);
            if (bt == 0)
                return "";
            string s = bt.ToString() + "字节";
            if (bt > 1024)
            {
                s = bt / 1024 + "KB";
            }

            if (bt > 1024 * 1024)
            {
                s = bt / 1024 * 1024 + "MB";
            }
            return s;
        }

        protected void lockPhoto_Click(object sender, EventArgs e)
        {
            if (Request.Form["photochkedid"] == null)
            {
                Message = MessageBox("请勾选需要操作的项", MsgType.warning);
            }
            else
            {
                string ids = Request.Form["photochkedid"];
                string[] id = ids.Split(',');
                Jy123.Model.User_Photo modelPhoto = new Jy123.Model.User_Photo();
                Jy123.BLL.User_Photo bllPhoto = new Jy123.BLL.User_Photo();
                for (int i = 0; i < id.Length; i++)
                {
                    modelPhoto = bllPhoto.GetModel(Convert.ToInt32(id[i]));
                    modelPhoto.IsLock = true;
                    bllPhoto.Update(modelPhoto);
                    //p.Lock(Convert.ToInt32(id[i]));
                }
                //Response.Redirect(Request.RawUrl);
                Response.Redirect("Photo.aspx");
                Message = MessageBox("已锁定", MsgType.ok);
            }            
        }
        protected void unlockPhoto_Click(object sender, EventArgs e)
        {
            if (Request.Form["photochkedid"] == null)
            {
                Message = MessageBox("请勾选需要操作的项", MsgType.warning);
            }
            else
            {
                string ids = Request.Form["photochkedid"];
                string[] id = ids.Split(',');
                Jy123.Model.User_Photo modelPhoto = new Jy123.Model.User_Photo();
                Jy123.BLL.User_Photo bllPhoto = new Jy123.BLL.User_Photo();
                for (int i = 0; i < id.Length; i++)
                {
                    modelPhoto = bllPhoto.GetModel(Convert.ToInt32(id[i]));
                    modelPhoto.IsLock = false;
                    bllPhoto.Update(modelPhoto);
                    //p.Lock(Convert.ToInt32(id[i]));
                }
                Response.Redirect(Request.RawUrl);
                Message = MessageBox("已解除锁定", MsgType.ok);                
            }            
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (Key.Text != null)
            {
                count = new List<int?>();
                RptData.DataSource = new BLL.User.Album().ManagePhotoList(1, pagesize, "UserName like '%" + Key.Text.Trim() + "%'",out count);
                RptData.DataBind();
            }
        }

        //删除图片
        Jy123.Model.User_Photo modelPhoto = new Jy123.Model.User_Photo();
        Jy123.BLL.User_Photo bllPhoto = new Jy123.BLL.User_Photo();
        protected void btnDel_Click(object sender, EventArgs e)
        {
            //Common.Public.DelFile(Server.MapPath("/")+"/JyIMG/User/2009/6/4/1/User_633797275032500000.bmp");
            if (Request.Form["photochkedid"] == null)
            {
                Message = MessageBox("请勾选需要操作的项", MsgType.warning);
            }
            else
            {
                string ids = Request.Form["photochkedid"];
                string[] id = ids.Split(',');

                bool delSuccess = false;
                //循环删除物理文件
                for (int i = 0; i < id.Length; i++)
                {                    
                    modelPhoto = bllPhoto.GetModel(int.Parse(id[i]));
                    delSuccess = Common.Public.DelFile(Server.MapPath("/") + modelPhoto.FilePath);
                    if (!delSuccess)
                    {
                        break;
                    }
                }
                if (delSuccess == true)
                {
                    //删除数据库中的记录
                    new BLL.User.Album().DelPhoto(ids);
                }
                
                Response.Redirect(Request.RawUrl);
            }            
        }
    }
}
